﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Controllers
{
    [LoginAttribute(IsNeed = false)]
    public class LoginController : BaseController
    {
        // GET: Login
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 验证码
        /// </summary>
        /// <returns>返回验证码</returns>
        public ActionResult CheckCode()
        {
            //首先实例化验证码的类
            ValidateCode validateCode = new ValidateCode();
            //生成验证码指定的长度
            string code = validateCode.CreateValidateCode(5);
            //保存验证码
            this.TempData["ValidateCode"] = code;
            //创建验证码的图片
            byte[] bytes = validateCode.CreateValidateGraphic(code);
            //最后将验证码返回
            return File(bytes, @"image/jpeg");
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <param name="yzm"></param>
        /// <returns></returns>
        public ActionResult DoAction(string userName, string userPassword, string yzm)
        {
            OperateResult resultInfo = new OperateResult();
            //if (String.IsNullOrWhiteSpace(yzm))
            //{
            //    resultInfo.Result = 0;
            //    resultInfo.Body = "验证码不能为空";
            //}
            //try
            //{
            //    string vcode = TempData["ValidateCode"].ToString();
            //    if (yzm.Trim() != vcode)
            //    {
            //        resultInfo.Result = 0;
            //        resultInfo.Body = "验证码不正确";
            //    }
            //}
            //catch
            //{
            //    resultInfo.Result = 0;
            //    resultInfo.Body = "验证码不正确";
            //}
            if (resultInfo.Result == 1)
            {
                resultInfo = LoginManager.Login(userName, userPassword);
            }

            return JsonDate(resultInfo);
        }

        /// <summary>
        /// 退出系统
        /// </summary>
        /// <returns></returns>
        public void LoginOut()
        {
            LoginManager.Logout(); 
        }

        /// <summary>
        /// 错误页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Error()
        {
            return View();
        }
    }
}